package tr.com.netgis;

import tr.com.netgis.web.SendMessageRequest;
import tr.com.netgis.web.SendMessageResponse;
import tr.com.netgis.web.WebServisCaller;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 
 * @author NetGis
 *
 */
public class LocalMessageViewActivity extends Activity {

	private Button btnSendMessage;
	private EditText txtMessage;

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_local_message);
		
		btnSendMessage = (Button)findViewById(R.id.ng_new_local_message_btnSend);
		txtMessage = (EditText)findViewById(R.id.ng_new_local_message_txtMessage);
		btnSendMessage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog = ProgressDialog.show(LocalMessageViewActivity.this, "",
						"Birkaç saniye bekleyiniz, mesaj gönderiliyor...",
						true);
				new SendTask().execute();
			}
		});
		
		TextWatcher tw = new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				btnSendMessage.setEnabled(s.length() > 0);				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				btnSendMessage.setEnabled(s.length() > 0);
			}
		};
		txtMessage.addTextChangedListener(tw);

	}
	
	ProgressDialog dialog;

	private void dismissProgress() {
		dialog.dismiss();
	}
	
	class SendTask extends AsyncTask<Void, Void, String> {

		protected String doInBackground(Void... vs) {
			SendMessageResponse response = WebServisCaller.callWebService(new SendMessageRequest()); //TODO mesajı oluştur
			return "Mesaj Gönderildi " + response; //TODO dönen mesajı kontrol et
		}

		protected void onProgressUpdate(Void... progress) {
		}

		protected void onPostExecute(String result) {
			Toast toast = Toast.makeText(NGApplication.getAppContext(), result, 3000);
			toast.show();
			dismissProgress();
			LocalMessageViewActivity.this.finish();
		}
	};	

}
